<?php
class IndexController  extends Core\Api{
    public function indexAction(){
        echo '恭喜您接口连接成功';
    }
    public function loginAction(){
        $loginname = $this->post('loginname');
        $password = $this->post('password');
        $user = \Business\UserModel::login($loginname,$password);
        if ($user) {
            $this->session['user'] = $user;
            $ret = array(
                'status' => 1,
                'message' =>'登陆成功',
            );
            $ret['data'] = $user;
            $ret['_token_'] = session_id();
            //print_R($this->session);
        } else {
            $ret = array(
                'status' => 2003,
                'message' =>'用户名或密码错误',
            );
        }
        $this->json($ret);
        return;
    }
    public function logoutAction(){
        if($this->session['user']){
            $this->session['user'] = null;
        }
        $ret = $this->ok();
        $this->json($ret);
        return;
    }
    public function regAction(){
        $email = $this->post('email');
        $phone = $this->post('phone');
        $nickname = $this->post('nickname');
        //$realkname = $this->post('realname');
        $password = $this->post('password');
        if (!$email && !$phone){
            $ret = $this->error('邮箱或手机不能为空');
            return $this->json($ret);
        }
        if (!$nickname){
            $ret = $this->error('昵称不能为空');
            return $this->json($ret);
        }
        if (!$password){
            $ret = $this->error('密码不能为空');
            return $this->json($ret);
        }
        if ($phone) {
            $vcode = $this->post('vcode');
            if (!$vcode) {
                $ret = $this->error('验证码不能为空');
                return $this->json($ret);
            }
            $vc = VcodeModel::where('phone',$phone)
                ->where('code',$vcode)
                ->first();
            if (!$vc) {
                $ret = $this->error('验证码不正确');
                return $this->json($ret);
            }
            $params['phone'] = $phone;
        }
        if ($email) {
            $params['email'] = $email;
        }
        $params['nickname'] = $nickname;
        //$params['realname'] = $realname;
        $params['password'] = $password;
        try{
            $s = \Business\UserModel::reg($params);
        }catch(Exception $e){
            //$e->getCode();
            $ret = $this->error('用户已存在');
            return $this->json($ret);
        }
        if($s){
            $ret = $this->ok();
        }else{
            $ret = $this->error('注册失败');
        }
        return $this->json($ret);
    }
    public function sendCodeToPhoneAction(){
        $phone = $this->post('phone');
        $type = $this->post('type','reg');
        if (!$phone) {
            $ret = $this->error('手机号不能为空');
            return $this->json($ret);
        }
        $code = rand(100000,999999);
        $vcode = new VcodeModel();
        $vcode->phone = $phone;
        $vcode->code = $code;
        $vcode->type = $type;
        if ($vcode->save()) {
            $s = SMS::send($vcode->phone,'您的验证码为'.$code.'请妥善保管，若非本人操作，请忽略');
            $ret = $this->ok();
            return $this->json($ret);
        } else {
            $ret = $this->error('发送失败');
            return $this->json($ret);
        }
    }
    public function restPasswordAction() {
        $phone = $this->post('phone');
        $email = $this->post('email');
        $vcode = $this->post('vcode');
        $password = $this->post('password');

        if (!$phone && !$email) {
            $ret = $this->error('手机号有邮箱没有填写');
        }
        if (empty($password)) {
            return $this->error('密码不能为空');
        }
        if ($phone) {
            $res = VcodeModel::where('phone',$phone)->orderBy('id','desc')->first();
            $user = UserModel::where('phone',$phone)->first();
        } else {
            //这里数据库还没那个字段哦
            $res = VcodeModel::where('email',$email)->orderBy('id','desc')->first();
        }
        if (!$user) {
            $ret = $this->error();
            return $this->json($ret);
        }
        if ($res->code != $vcode) {
            $ret = $this->error('验证码错误');
            return $this->json($ret);
        }
        $user->password = $password;
        if ($user->save()) {
            $ret = $this->ok();
        }else {
            $ret = $this->error();
        }
        return $this->json($ret);
    }
    public function tAction(){
        /*
        $i = file_get_contents('php://input');
        print_R($_SERVER['HTTP_CONTENT_TYPE']);
        if ($_SERVER['HTTP_CONTENT_TYPE'] == 'multipart/form-data') {

        } else if($_SERVER['HTTP_CONTENT_TYPE'] == 'application/x-www-form-urlencoded'){

        }
        echo $i;
         */
        \Business\ProductModel::countData();
    }
}
